﻿using MediatR;
using Order.Api.Read.Application.Command;
using Order.Api.Read.Dto.Order;
using Order.Api.Read.Service.DrugOrdersService;
using Order.ErrorCode;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Order.Api.Read.Application.Handler
{
    /// <summary>
    /// 药品订单分页处理器
    /// </summary>
    public class DrugOrdersPageHandler : IRequestHandler<DrugOrdersPageCommand, ApiPageIng<DrugOrdersPageDto>>
    {
        private readonly IDrugOrdersService _drugOrdersService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="drugOrdersService">药品订单服务</param>
        public DrugOrdersPageHandler(IDrugOrdersService drugOrdersService)
        {
            _drugOrdersService = drugOrdersService ?? throw new ArgumentNullException(nameof(drugOrdersService), "药品订单服务不能为空");
        }

        /// <summary>
        /// 处理药品订单分页查询请求
        /// </summary>
        /// <param name="request">分页查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>分页结果</returns>
        public async Task<ApiPageIng<DrugOrdersPageDto>> Handle(DrugOrdersPageCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 参数验证
                if (request == null)
                {
                    throw new ArgumentNullException(nameof(request), "分页查询请求不能为空");
                }

                // 调用服务层获取分页数据
                var result = await _drugOrdersService.GetDrugOrdersPageAsync(request);
                return result;
            }
            catch (Exception ex)
            {
                // 记录异常日志
                // TODO: 添加日志记录
                throw new Exception($"处理药品订单分页查询请求时发生错误: {ex.Message}", ex);
            }
        }
    }

    /// <summary>
    /// 药品订单详情处理器
    /// </summary>
    public class DrugOrdersDetailHandler : IRequestHandler<DrugOrdersDetailCommand, DrugOrdersDetailDto>
    {
        private readonly IDrugOrdersService _drugOrdersService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="drugOrdersService">药品订单服务</param>
        public DrugOrdersDetailHandler(IDrugOrdersService drugOrdersService)
        {
            _drugOrdersService = drugOrdersService ?? throw new ArgumentNullException(nameof(drugOrdersService), "药品订单服务不能为空");
        }

        /// <summary>
        /// 处理药品订单详情查询请求
        /// </summary>
        /// <param name="request">详情查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>订单详情</returns>
        public async Task<DrugOrdersDetailDto> Handle(DrugOrdersDetailCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 参数验证
                if (request == null)
                {
                    throw new ArgumentNullException(nameof(request), "详情查询请求不能为空");
                }

                // 调用服务层获取详情数据
                var result = await _drugOrdersService.GetDrugOrdersDetailAsync(request);
                return result;
            }
            catch (Exception ex)
            {
                // 记录异常日志
                // TODO: 添加日志记录
                throw new Exception($"处理药品订单详情查询请求时发生错误: {ex.Message}", ex);
            }
        }
    }
}
